var express = require('express');
var path = require('path');
var db = require('./db.js');
var app = express();

app.set('port',process.env.PORT || 3000);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(express.static(path.join(__dirname, 'public')));

app.get('/mysql',function(req,res,next){
  var client = db.createConn();
  db.findUser(client,function(errs,results){
    if (errs) {
      console.log("errs..........");
      res.status(500).send('Something has wrong!');
    }
    if(results){
      res.render('index',{results: results});
    }
  });
});

// 定制404页面
app.use(function(req,res){
	res.writeHeader(404,{"content-type":"text/plain"});
	res.end("404 - NOT FOUND");
});

// 定制500页面
app.use(function(req,res){
	res.writeHeader(500,{"content-type":"text/plain"});
	res.send("500 - Server Error");
});

app.listen(app.get('port'),function(){
	console.log('Express started on http://localhost:'+ app.get('port'));
});